﻿using System;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCVideoNewest : System.Web.UI.UserControl
	{
		private IVideoProvider _VideoProvider;

		public string ServerURL
		{
			get
			{
				if(Request.Url.Port > 0)
				{
					return string.Format("http://{0}:{1}", Request.Url.Host, Request.Url.Port.ToString());
				}
				else
				{
					return "http://" + Request.Url.Host;
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			//if(!IsPostBack)
			{
				BindVideo();
			}
		}

		protected void rptVideo_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			if(e.Item.DataItem != null)
			{
				var dataItem = e.Item.DataItem as DBProvider.Video;
				var lblVideoTitle = e.Item.FindControl("lblVideoTitle") as System.Web.UI.WebControls.Label;

				lblVideoTitle.ToolTip = dataItem.VideoTitle;

				if(dataItem.VideoTitle.Length > 9)
				{
					lblVideoTitle.Text = dataItem.VideoTitle.Substring(0, 8) + "...";
				}
				else
				{
					lblVideoTitle.Text = dataItem.VideoTitle;
				}
			}
		}

		private void BindVideo()
		{
			if(_VideoProvider == null)
			{
				_VideoProvider = new VideoProvider();
			}

			var user = SiteContext.Current.CurrentUser;

			if(user != null && user.Role != null && user.Role.RoleName.Equals("admin"))
			{
				var drugStore = SiteContext.Current.CurrentDrugStore;

				if(drugStore != null)
				{
					rptVideo.DataSource = _VideoProvider.GetNewVideos(5, drugStore.DrugStoreID);
					rptVideo.DataBind();
				}
			}
			else
			{
				if(user != null && user.DrugStoreID.GetValueOrDefault(0) > 0)
				{
					rptVideo.DataSource = _VideoProvider.GetNewVideos(5, user.DrugStoreID.GetValueOrDefault(0), user.UserID);
					rptVideo.DataBind();
				}
			}
		}
	}
}